home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / SpreadSheet / SpreadSheet.class (.txt) < prev    next >
Encoding:
Java Class File  |  1995-10-12  |  6.7 KB  |  285 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8.  
  9. public class SpreadSheet extends Applet {
  10.    String title;
  11.    Font titleFont;
  12.    Color cellColor;
  13.    Color inputColor;
  14.    int cellWidth = 100;
  15.    int cellHeight = 15;
  16.    int titleHeight = 15;
  17.    int rowLabelWidth = 15;
  18.    Font inputFont;
  19.    boolean isStopped = false;
  20.    boolean fullUpdate = true;
  21.    int rows;
  22.    int columns;
  23.    int currentKey = -1;
  24.    int selectedRow = -1;
  25.    int selectedColumn = -1;
  26.    SpreadSheetInput inputArea;
  27.    Cell[][] cells;
  28.    Cell current;
  29.  
  30.    public synchronized void init() {
  31.       this.cellColor = Color.white;
  32.       this.inputColor = new Color(100, 100, 225);
  33.       this.inputFont = new Font("Courier", 0, 10);
  34.       this.titleFont = new Font("Courier", 1, 12);
  35.       this.title = ((Applet)this).getParameter("title");
  36.       if (this.title == null) {
  37.          this.title = "Spreadsheet";
  38.       }
  39.  
  40.       String rs = ((Applet)this).getParameter("rows");
  41.       if (rs == null) {
  42.          this.rows = 9;
  43.       } else {
  44.          this.rows = Integer.parseInt(rs);
  45.       }
  46.  
  47.       rs = ((Applet)this).getParameter("columns");
  48.       if (rs == null) {
  49.          this.columns = 5;
  50.       } else {
  51.          this.columns = Integer.parseInt(rs);
  52.       }
  53.  
  54.       this.cells = new Cell[this.rows][this.columns];
  55.       char[] l = new char[1];
  56.  
  57.       for(int i = 0; i < this.rows; ++i) {
  58.          for(int j = 0; j < this.columns; ++j) {
  59.             this.cells[i][j] = new Cell(this, Color.lightGray, Color.black, this.cellColor, this.cellWidth - 2, this.cellHeight - 2);
  60.             l[0] = (char)(97 + j);
  61.             rs = ((Applet)this).getParameter("" + new String(l) + (i + 1));
  62.             if (rs != null) {
  63.                this.cells[i][j].setUnparsedValue(rs);
  64.             }
  65.          }
  66.       }
  67.  
  68.       Dimension d = ((Component)this).size();
  69.       this.inputArea = new SpreadSheetInput((String)null, this, d.width - 2, this.cellHeight - 1, this.inputColor, Color.white);
  70.       ((Component)this).resize(this.columns * this.cellWidth + this.rowLabelWidth, (this.rows + 1) * this.cellHeight + this.cellHeight + this.titleHeight);
  71.    }
  72.  
  73.    public void setCurrentValue(float val) {
  74.       if (this.selectedRow != -1 && this.selectedColumn != -1) {
  75.          this.cells[this.selectedRow][this.selectedColumn].setValue(val);
  76.          ((Component)this).repaint();
  77.       }
  78.    }
  79.  
  80.    public void stop() {
  81.       this.isStopped = true;
  82.    }
  83.  
  84.    public void start() {
  85.       this.isStopped = false;
  86.    }
  87.  
  88.    public void destroy() {
  89.       for(int i = 0; i < this.rows; ++i) {
  90.          for(int j = 0; j < this.columns; ++j) {
  91.             if (this.cells[i][j].type == 2) {
  92.                this.cells[i][j].updaterThread.stop();
  93.             }
  94.          }
  95.       }
  96.  
  97.    }
  98.  
  99.    public void setCurrentValue(int type, String val) {
  100.       if (this.selectedRow != -1 && this.selectedColumn != -1) {
  101.          this.cells[this.selectedRow][this.selectedColumn].setValue(type, val);
  102.          ((Component)this).repaint();
  103.       }
  104.    }
  105.  
  106.    public void update(Graphics g) {
  107.       if (this.fullUpdate) {
  108.          this.paint(g);
  109.          this.fullUpdate = false;
  110.       } else {
  111.          g.setFont(this.titleFont);
  112.  
  113.          for(int i = 0; i < this.rows; ++i) {
  114.             for(int j = 0; j < this.columns; ++j) {
  115.                if (this.cells[i][j].needRedisplay) {
  116.                   int cx = j * this.cellWidth + 2 + this.rowLabelWidth;
  117.                   int cy = (i + 1) * this.cellHeight + 2 + this.titleHeight;
  118.                   this.cells[i][j].paint(g, cx, cy);
  119.                }
  120.             }
  121.          }
  122.  
  123.       }
  124.    }
  125.  
  126.    public void recalculate() {
  127.       for(int i = 0; i < this.rows; ++i) {
  128.          for(int j = 0; j < this.columns; ++j) {
  129.             if (this.cells[i][j] != null && this.cells[i][j].type == 3) {
  130.                this.cells[i][j].setRawValue(this.evaluateFormula(this.cells[i][j].parseRoot));
  131.                this.cells[i][j].needRedisplay = true;
  132.             }
  133.          }
  134.       }
  135.  
  136.       ((Component)this).repaint();
  137.    }
  138.  
  139.    public float evaluateFormula(Node n) {
  140.       float val = 0.0F;
  141.       if (n == null) {
  142.          return val;
  143.       } else {
  144.          switch (n.type) {
  145.             case 0:
  146.                val = this.evaluateFormula(n.left);
  147.                switch (n.op) {
  148.                   case '*':
  149.                      val *= this.evaluateFormula(n.right);
  150.                      return val;
  151.                   case '+':
  152.                      val += this.evaluateFormula(n.right);
  153.                      return val;
  154.                   case ',':
  155.                   case '.':
  156.                   default:
  157.                      return val;
  158.                   case '-':
  159.                      val -= this.evaluateFormula(n.right);
  160.                      return val;
  161.                   case '/':
  162.                      val /= this.evaluateFormula(n.right);
  163.                      return val;
  164.                }
  165.             case 1:
  166.                return n.value;
  167.             case 2:
  168.                if (n != null && this.cells[n.row][n.column] != null) {
  169.                   return this.cells[n.row][n.column].value;
  170.                }
  171.          }
  172.  
  173.          return val;
  174.       }
  175.    }
  176.  
  177.    public synchronized void paint(Graphics g) {
  178.       char[] l = new char[1];
  179.       Dimension d = ((Component)this).size();
  180.       g.setFont(this.titleFont);
  181.       int i = g.getFontMetrics().stringWidth(this.title);
  182.       g.drawString(this.title == null ? "Spreadsheet" : this.title, (d.width - i) / 2, 12);
  183.       g.setColor(this.inputColor);
  184.       g.fillRect(0, this.cellHeight, d.width, this.cellHeight);
  185.       g.setFont(this.titleFont);
  186.  
  187.       for(int i = 0; i < this.rows + 1; ++i) {
  188.          int cy = (i + 2) * this.cellHeight;
  189.          g.setColor(((Component)this).getBackground());
  190.          g.draw3DRect(0, cy, d.width, 2, true);
  191.          if (i < this.rows) {
  192.             g.setColor(Color.red);
  193.             g.drawString("" + (i + 1), 2, cy + 12);
  194.          }
  195.       }
  196.  
  197.       g.setColor(Color.red);
  198.  
  199.       for(int i = 0; i < this.columns; ++i) {
  200.          int cx = i * this.cellWidth;
  201.          g.setColor(((Component)this).getBackground());
  202.          g.draw3DRect(cx + this.rowLabelWidth, 2 * this.cellHeight, 1, d.height, true);
  203.          if (i < this.columns) {
  204.             g.setColor(Color.red);
  205.             l[0] = (char)(65 + i);
  206.             g.drawString(new String(l), cx + this.rowLabelWidth + this.cellWidth / 2, d.height - 3);
  207.          }
  208.       }
  209.  
  210.       for(int i = 0; i < this.rows; ++i) {
  211.          for(int j = 0; j < this.columns; ++j) {
  212.             int cx = j * this.cellWidth + 2 + this.rowLabelWidth;
  213.             int cy = (i + 1) * this.cellHeight + 2 + this.titleHeight;
  214.             if (this.cells[i][j] != null) {
  215.                this.cells[i][j].paint(g, cx, cy);
  216.             }
  217.          }
  218.       }
  219.  
  220.       g.setColor(((Component)this).getBackground());
  221.       g.draw3DRect(0, this.titleHeight, d.width, d.height - this.titleHeight, false);
  222.       this.inputArea.paint(g, 1, this.titleHeight + 1);
  223.    }
  224.  
  225.    public boolean mouseDown(Event evt, int x, int y) {
  226.       if (y < this.titleHeight + this.cellHeight) {
  227.          this.selectedRow = -1;
  228.          if (y <= this.titleHeight && this.current != null) {
  229.             this.current.deselect();
  230.             this.current = null;
  231.          }
  232.  
  233.          return true;
  234.       } else if (x < this.rowLabelWidth) {
  235.          this.selectedRow = -1;
  236.          if (this.current != null) {
  237.             this.current.deselect();
  238.             this.current = null;
  239.          }
  240.  
  241.          return true;
  242.       } else {
  243.          this.selectedRow = (y - this.cellHeight - this.titleHeight) / this.cellHeight;
  244.          this.selectedColumn = (x - this.rowLabelWidth) / this.cellWidth;
  245.          if (this.selectedRow <= this.rows && this.selectedColumn < this.columns) {
  246.             if (this.selectedRow >= this.rows) {
  247.                this.selectedRow = -1;
  248.                if (this.current != null) {
  249.                   this.current.deselect();
  250.                   this.current = null;
  251.                }
  252.  
  253.                return true;
  254.             }
  255.  
  256.             Cell cell = this.cells[this.selectedRow][this.selectedColumn];
  257.             this.inputArea.setText(new String(cell.getPrintString()));
  258.             if (this.current != null) {
  259.                this.current.deselect();
  260.             }
  261.  
  262.             this.current = cell;
  263.             this.current.select();
  264.             ((Component)this).requestFocus();
  265.             this.fullUpdate = true;
  266.             ((Component)this).repaint();
  267.          } else {
  268.             this.selectedRow = -1;
  269.             if (this.current != null) {
  270.                this.current.deselect();
  271.                this.current = null;
  272.             }
  273.          }
  274.  
  275.          return true;
  276.       }
  277.    }
  278.  
  279.    public boolean keyDown(Event evt, int key) {
  280.       this.fullUpdate = true;
  281.       this.inputArea.keyDown(key);
  282.       return true;
  283.    }
  284. }
  285.